Storage.prototype.setObject = function (key, value) {
	'use strict';
	try {
		this.setItem(key, JSON.stringify(value));
	} catch (e) {
	 	 if (e === QUOTA_EXCEEDED_ERR) {
	 	 	 alert('Local storage quota exceeded!');
		}
	}
};
Storage.prototype.getObject = function (key) {
	'use strict';
	var value = this.getItem(key);
	return value && JSON.parse(value);
};
Array.prototype.moveIndex = function (index, steps) {
	'use strict';
	var i, newIdx, tmp = [];
	newIdx = index + steps;
	if (newIdx >= this.length) {
		newIdx = this.length - 1;
	}
	if (newIdx < 0) {
		newIdx = 0;
	}
	for (i = 0; i < this.length; i++) {
        if (steps < 0) {
			if (i === newIdx) {
				tmp.push(this[index]);
			}
		}
		if (i !== index) {
			tmp.push(this[i]);
		}
        if (steps > 0) {
			if (i === newIdx) {
				tmp.push(this[index]);
			}
		}
	}
	for (i = 0; i < tmp.length; i++) {
	    this[i] = tmp[i];
	}
	tmp = null;
	return this;
};
var objStor = function (storageKey) {
	'use strict';
	var obj, ls = {
		storageKey: null,
		set: function (obj) {
			if (this.hasLocalStorage()) {
				localStorage.setObject(this.storageKey, obj);
			}
		},
		get: function () {
			var objAry;
			if (this.hasLocalStorage()) {
				var objAry = localStorage.getObject(this.storageKey);
			}
			if (!objAry) {
				objAry = [];
			}
			return objAry;
		},
		hasLocalStorage: function () {
			return (typeof(localStorage) === 'undefined' ) ? false : true;
		}
	};
	obj = {
		findIndex: function (uid) {
			var i, d = ls.get();
			for (i = 0; i < d.length; i++) {
				if (d[i].uid === uid) {
					return i;
				}
			}
			return false;
		},
		moveIndex: function (index, steps) {
			var d = ls.get();
			d.moveIndex(index, steps);
			ls.set(d);
		},
		removeIndex: function (index) {
			var i, tmp = [], d = ls.get();
			for (i = 0; i < d.length; i++) {
				if (i !== index) {
					tmp.push(d[i]);
				}
			}
			ls.set(tmp);
		},
		ui: {
			hasUid: function (uid) {
				return (obj.findIndex(uid) !== false) ? true : false;
			},
			get: function (uid) {
				var d = ls.get();
				if (!isNaN(uid)) {
					return d[obj.findIndex(uid)];
				}
				return d;
			},
			getUids: function () {
				var tmp = [], i, d = ls.get();
				for (i = 0; i < d.length; i++) {
					tmp.push(d[i].uid);
				}
				return tmp;
			},
			set: function (uid) {
				if (!this.hasUid(uid)) {
					var d = ls.get();
					d.push({"uid": uid});
					ls.set(d);
				}
			},
			toggle: function (uid) {
				if (!this.hasUid(uid)) {
					this.set(uid);
					return true;
				} else {
					this.remove(uid);
					return false;
				}
			},
			update: function (uid, objAttr) {
				var i, item, d;
				if (this.hasUid(uid)) {
					d = ls.get();
					for (i = 0; i < d.length; i++) {
						if (d[i].uid === uid) {
							for (item in objAttr) {
								if (objAttr.hasOwnProperty(item) && item !== 'uid') {
									d[i][item] = objAttr[item];
								}
							}
							ls.set(d);
							return;
						}
					}
				}
			},
			remove: function (uid) {
				obj.removeIndex(obj.findIndex(uid));
			},
			up: function (uid) {
				obj.moveIndex(obj.findIndex(uid), -1);
			},
			down: function (uid) {
				obj.moveIndex(obj.findIndex(uid), 1);
			},
			clearAll: function () {
				ls.set([]);
			}
		}
	};
	if (storageKey) {
		ls.storageKey = storageKey;
	}
	return obj.ui;
};

var myFavorites = objStor('favorites');
var myVisits = objStor('visits');

/*
	obj = [
		{"uid": 78798, "var":"val", "var":"val"},
		{"uid": 78798, "var":"val"},
		{"uid": 78798, "var":"val"},
		{"uid": 78798, "var":"val", "var":"val"},
		{"uid": 78798, "var":"val"},
		{"uid": 78798, "var":"val"},
		{"uid": 78798, "var":"val", "var":"val"},
		{"uid": 78798, "var":"val"},
		{"uid": 78798, "var":"val"},
	]
*/